package com.zhz.lib.project.service.news;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhz.lib.project.hibernate.Sortmap;
import com.zhz.lib.project.ibatis.daointerface.NewsDAO;
import com.zhz.lib.project.ibatis.dataobject.News;
import com.zhz.lib.project.vo.news.NewsVO;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午08:42:32
 * 
 */
@Service
public class NewsService {
	private static Logger logger = Logger.getLogger(NewsService.class);
	@Autowired
	private BaseDAO baseDao;
	@Autowired
	private NewsDAO newsDAO;

	// 最新消息类型
	private static Integer newstypefatherid = 1;
	private static int MAX_LENGTH = 35;

	/**
	 * 获取最新消息
	 * 
	 * 最新消息的条件：<br/>
	 * 1、未被删除 ifDelete =0<br/>
	 * 2、审核通过aproveState=1<br/>
	 * 3、发布时间早于当天<br/>
	 * 
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public List<NewsVO> getLatestNews(int firstResult, int maxResults) {

		List<News> newsList = newsDAO.getLatestNews(firstResult, maxResults);
		List<NewsVO> newsVOList = new ArrayList<NewsVO>();
		if (newsList != null && newsList.size() > 0)
			for (News news : newsList) {
				NewsVO newsVO = new NewsVO();
				try {
					BeanUtils.copyProperties(newsVO, news);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("从news复制到newsVO时发生异常", e);
				}
				newsVO.setTitle(ZhzStringUtil.subStringWithDot(newsVO
						.getTitle(), MAX_LENGTH));
				newsVOList.add(newsVO);
			}
		return newsVOList;
	}

	/**
	 * 获取置顶的最新消息
	 * 
	 * 能够置顶的条件：<br/>
	 * 1、未被删除 ifDelete =0<br/>
	 * 2、设置为置顶 top=1<br/>
	 * 3、审核通过 aproveState=1<br/>
	 * 4、发布时间早于当天<br/>
	 * 5、"当天"在置顶时间区域内<br/>
	 * 
	 * @param firstResult
	 * @param maxResults
	 * @return
	 */
	public List<NewsVO> getTopNews(int firstResult, int maxResults) {

		List<News> newsList = newsDAO.getTopNews(firstResult, maxResults);
		List<NewsVO> newsVOList = new ArrayList<NewsVO>();
		if (newsList != null && newsList.size() > 0)
			for (News news : newsList) {
				NewsVO newsVO = new NewsVO();
				try {
					BeanUtils.copyProperties(newsVO, news);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("从news复制到newsVO时发生异常", e);
				}
				newsVO.setTitle(ZhzStringUtil.subStringWithDot(newsVO
						.getTitle(), MAX_LENGTH));
				newsVOList.add(newsVO);
			}
		return newsVOList;
	}

	@SuppressWarnings("unchecked")
	public List<Sortmap> getNewsType() {

		// 查询数据库，得到最新消息类型
		return (List<Sortmap>) baseDao.findbyorderlimit("Sortmap", "fatherId",
				Integer.toString(newstypefatherid), "order by sortMapId ASC");
	}

	/**
	 * 根据ID获取消息
	 * 
	 * @param hotlinkVO
	 * @return
	 */
	public NewsVO getNewsById(int id) {

		News news = newsDAO.getNewsById(id + "");
		NewsVO newsVO = new NewsVO();
		try {
			BeanUtils.copyProperties(newsVO, news);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("从news复制到newsVO时发生异常", e);
			return null;
		}
		Sortmap newsfrom = (Sortmap) baseDao.findOneObject("Sortmap",
				"sortMapId", news.getNewsfrom());
		newsVO.setNewsfrom(newsfrom.getValue());
		return newsVO;
	}

	/**
	 * 点击次数增加1
	 * 
	 * @param hotlinkVO
	 * @return
	 */
	public void newsHitPlusOne(int id) {
		newsDAO.newsHitPlusOne(id + "");
	}
}
